#!/usr/bin/env python
#-*- coding: utf-8 -*-


import json
from sqlalchemy.types import TypeDecorator, VARCHAR


class JsonType(TypeDecorator):

    """
    Represents an immutable structure as a json-encoded string.
    Usage::
        JsonType(255)
    """

    impl = VARCHAR

    def process_bind_param(self, value, dialect):
        if value is not None:
            value = json.dumps(value)
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = json.loads(value)
        return value
